﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entity = SeriesTheque.Data;
using System.Runtime.InteropServices;


namespace SeriesTheque.DataService
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom de classe "Service1" à la fois dans le code et le fichier de configuration.
    public class Service1 : IService1
    {
        public List<User> GetAllUsers()
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Users != null && context.Users.Count() > 0)
                    {
                        return Translator.Translate_ListUsersEntity_To_ListUsers(
                            context.Users.ToList()
                            );
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<User> GetAllUsersByEtat(Boolean enabled)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Users != null && context.Users.Count() > 0)
                    {
                        return Translator.Translate_ListUsersEntity_To_ListUsers(
                            context.Users.Where(x => x.User_Enable == enabled).ToList()
                            );
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<Serie> GetAllSeries()
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Series != null && context.Series.Count() > 0)
                    {
                        return Translator.Translate_ListSeriesEntity_To_ListSeries(
                            context.Series.ToList()
                            );
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<Serie> GetAllSeriesByEtat(Boolean enabled)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Series != null && context.Series.Count() > 0)
                    {
                        return Translator.Translate_ListSeriesEntity_To_ListSeries(
                            context.Series.Where(x => x.Serie_Enable == enabled).ToList()
                            );
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<Serie> GetSeriesByUser(string userLogin)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Series != null && context.Series.Count() > 0)
                    {
                        return Translator.Translate_ListSeriesEntity_To_ListSeries(
                            context.Series.Where(x => x.User.User_Login == userLogin).ToList()
                            );
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<Serie> GetSeriesByUserByEtat(string userLogin, Boolean enabled)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Series != null && context.Series.Count() > 0)
                    {

                        return Translator.Translate_ListSeriesEntity_To_ListSeries(
                            context.Series.Where(x => x.User.User_Login == userLogin && x.Serie_Enable == enabled).ToList()
                            );

                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<Season> GetSeasonBySerie(int serieId)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Seasons != null && context.Seasons.Count() > 0)
                    {

                        return Translator.Translate_ListSeasonsEntity_To_ListSeasons(
                            context.Seasons.Where(x => x.Serie.Serie_Id == serieId).ToList()
                            );

                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<Season> GetSeasonBySerieAndEtat(int serieId, Boolean enabled)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Seasons != null && context.Seasons.Count() > 0)
                    {

                        return Translator.Translate_ListSeasonsEntity_To_ListSeasons(
                            context.Seasons.Where(x => x.Serie.Serie_Id == serieId && x.Season_Enable == enabled).ToList()
                            );

                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<Episode> GetEpisodesBySeason(int idseason)
        {
            using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
            {
                if (context.Episodes != null && context.Episodes.Count() > 0)
                {
                    return Translator.Translate_ListEpisodesEntity_To_ListEpisodes(
                        context.Episodes.Where(x => x.Season.Season_Id == idseason).ToList()
                        );
                }
                else
                {
                    return null;
                }
            }
        }

        public List<Episode> GetEpisodesBySeasonByEtat(int idserie, Boolean enabled)
        {
            using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
            {
                if (context.Episodes != null && context.Users.Count() > 0)
                {
                    return Translator.Translate_ListEpisodesEntity_To_ListEpisodes(context.Episodes.Where(x => x.Season.Season_Id == idserie && x.Episode_Enable == enabled).ToList());
                }

                else
                {
                    return null;
                }
            }
        }

        public List<Episode> GetEpisodesBySerie(int idserie)
        {
            using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
            {
                if (context.Episodes != null && context.Episodes.Count() > 0)
                {
                    return Translator.Translate_ListEpisodesEntity_To_ListEpisodes(
                        context.Episodes.Where(x => x.Season.Serie.Serie_Id == idserie).ToList()
                        );
                }
                else
                {
                    return null;
                }
            }
        }

        public List<Episode> GetEpisodesBySerieByEtat(int idserie, Boolean enabled)
        {
            using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
            {
                if (context.Episodes != null && context.Episodes.Count() > 0)
                {
                    return Translator.Translate_ListEpisodesEntity_To_ListEpisodes(
                        context.Episodes.Where(x => x.Season.Serie.Serie_Id == idserie && x.Episode_Enable == enabled).ToList()
                        );
                }
                else
                {
                    return null;
                }
            }
        }


        public Boolean IsUserExist(string Login)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Users.Where(x => x.User_Login == Login).ToList() != null && context.Users.Where(x => x.User_Login == Login).ToList().Count() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public Boolean IsSerieExist(int serieId)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Series.Where(x => x.Serie_Id == serieId).ToList() != null && context.Series.Where(x => x.Serie_Id == serieId).ToList().Count() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public Boolean IsSeasonExist(int seasonId)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Seasons.Where(x => x.Season_Id == seasonId).ToList() != null && context.Seasons.Where(x => x.Season_Id == seasonId).ToList().Count() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public Boolean IsEpisodeExist(int episodeId)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (context.Episodes.Where(x => x.Episode_Id == episodeId).ToList() != null && context.Episodes.Where(x => x.Episode_Id == episodeId).ToList().Count() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public Boolean IsUserValid(string login, string password)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    Entity.User user = null;
                    if (IsUserExist(login))
                    {
                        user = context.Users.Single(x => x.User_Login == login);
                    }

                    if (user != null)
                    {
                        if (PasswordHash.PasswordHash.ValidatePassword(password, user.User_Passwrd))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }

        }


        public Boolean ChangePasswordUser(string Login, string newPassword)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    Entity.User user = null;
                    if (IsUserExist(Login))
                    {
                        user = context.Users.Single(x => x.User_Login == Login);
                    }

                    if (user != null)
                    {
                        user.User_Passwrd = PasswordHash.PasswordHash.CreateHash(newPassword);
                        context.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }



        public Boolean AddUser(string Login, string password)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    Entity.User user = null;
                    if (IsUserExist(Login))
                    {
                        user = context.Users.Single(x => x.User_Login == Login);
                    }

                    if (user == null)
                    {
                        Entity.User newUser = new Entity.User();
                        newUser.User_Login = Login;
                        newUser.User_Passwrd = PasswordHash.PasswordHash.CreateHash(password);
                        newUser.User_date = DateTime.Now;
                        newUser.User_Enable = true;

                        context.Users.Add(newUser);
                        context.SaveChanges();

                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public Boolean AddSerie(int idserie, string seriename, string userlogin)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {

                    
                     if (!IsSerieExist(idserie))
                    {
                         
                        
                        if (IsUserExist(userlogin))
                            {
                                Entity.User user = new Entity.User();
                                user = context.Users.Single(x => x.User_Login == userlogin);

                                Entity.Serie newserie = new Entity.Serie();
                                newserie.Serie_Id = idserie;
                                newserie.Serie_Date = DateTime.Now;
                                newserie.Serie_Enable = true;
                                newserie.Serie_name = seriename;
                                newserie.User = user;

                                context.Series.Add(newserie);
                                context.SaveChanges();

                                return true; 
                            }
                        else 
                            {

                                return false;
                            }
                        }
                     else
	                {
                        return false;
	                }
              
                } 
            }    
            catch(Exception ex)
            {
                    return false;
            }
        }

        public Boolean AddSeason(int idseason, int idserie)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    

                        if (!IsSeasonExist(idseason))
                        {
                            if (IsSerieExist(idserie))
                            {
                                Entity.Serie serie = new Entity.Serie();
                                serie = context.Series.Single(x => x.Serie_Id == idserie);
                                Entity.Season newseason = new Entity.Season();
                                newseason.Season_Id = idseason;
                                newseason.Season_Date = DateTime.Now;
                                newseason.Season_Enable = true;
                                newseason.Serie = serie;
                                context.Seasons.Add(newseason);
                                context.SaveChanges();
                                return true;
                        }
                        else
                        {

                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public Boolean AddEpisode(int idepisode, string episodenumber, int idseason)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {


                    if (!IsEpisodeExist(idepisode))
                    {
                        if (IsSeasonExist(idseason))
                        {
                            Entity.Season season = new Entity.Season();
                            season = context.Seasons.Single(x => x.Season_Id == idseason);
                            Entity.Episode newepisode = new Entity.Episode();
                            newepisode.Episode_Id = idepisode;
                            newepisode.Episode_Number = episodenumber;
                            newepisode.Episode_Date = DateTime.Now;
                            newepisode.Episode_Enable = true;
                            newepisode.Season = season;
                            context.Episodes.Add(newepisode);
                            context.SaveChanges();
                            return true;
                        }
                        else
                        {

                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }



        public Boolean ChangeEtatSeason(int idseason, bool etat)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (IsSerieExist(idseason))
                    {
                        Entity.Season season = new Entity.Season();
                        season = context.Seasons.Single(x => x.Season_Id == idseason);
                        season.Season_Enable = etat;
                        context.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }

        }
        
        public Boolean ChangeEtatEpisode(int idepisode, bool etat)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    if (IsEpisodeExist(idepisode))
                    {
                        Entity.Episode episode = new Entity.Episode();
                        episode = context.Episodes.Single(x => x.Episode_Id == idepisode);
                        episode.Episode_Enable = etat;
                        context.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        public Boolean ChangeEtatSerie(int idserie, bool etat)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {

                    if (IsSerieExist(idserie))
                    {
                        Entity.Serie serie = new Entity.Serie();

                        serie = context.Series.Single(x => x.Serie_Id == idserie);
                        serie.Serie_Enable = etat;
                        context.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        public Boolean ChangeEtatUser(string Login, Boolean enable)
        {
            try
            {
                using (Entity.SeriesThequeModeleContainer context = new Entity.SeriesThequeModeleContainer())
                {
                    Entity.User user = null;
                    if (IsUserExist(Login))
                    {
                        user = context.Users.Single(x => x.User_Login == Login);
                    }

                    if (user != null)
                    {
                        user.User_Enable = enable;
                        context.SaveChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }


    }
}